#!/usr/bin/haserl


<?
main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html

. /usr/share/libubox/jshn.sh
json_init
json_load "`$DOCUMENT_ROOT/apps/disk/disk.sbin get_disk_info_json`"

anon_swap=`uci get fstab.@global[0].anon_swap`
anon_mount=`uci get fstab.@global[0].anon_mount`
check_fs=`uci get fstab.@global[0].check_fs`
cat <<EOF
<script>
\$(function(){
  \$('#post_disk_basesetting').on('submit', function(e){
    e.preventDefault();
    var data = "app=disk&action=post_disk_basesetting&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_disk_basesetting');
  });
});
</script>
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1">
				<h3>基础设置</h3>
				<form class="pure-form" id="post_disk_basesetting">
				<table class="pure-table pure-table-bordered">
						<tr>
							<td>
								自动挂载磁盘
							</td>
							<td>
								<select name="anon_swap">
									<option value="0" `[ ${anon_swap} -ne 1 ] && echo selected`>关</option>
									<option value="1" `[ ${anon_swap} -eq 1 ] && echo selected`>开</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>
								自动挂载交换分区
							</td>
							<td>
								<select name="anon_mount">
									<option value="0" `[ ${anon_mount} -ne 1 ] && echo selected`>关</option>
									<option value="1" `[ ${anon_mount} -eq 1 ] && echo selected`>开</option>
								</select>	
							</td>
						</tr>
						<tr>
							<td>
								自动检测文件系统
							</td>
							<td>
								<select name="check_fs">
									<option value="0" `[ ${check_fs} -ne 1 ] && echo selected`>关</option>
									<option value="1" `[ ${check_fs} -eq 1 ] && echo selected`>开</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>
								操作
							</td>
							<td>
								<button type="submit" class="pure-button">保存</button>
							</td>
						</tr>
				</table>
				</form>
				<h3>磁盘设置</h3>
EOF
cat <<EOF
	<table class="pure-table pure-table-bordered">
		<thead>
			<tr>
				<th>设备信息</th>
				<th>储存详情</th>
				<th>挂载点</th>
			</tr>
		</thead>

		<tbody>
EOF
json_select "disk_info"
json_get_keys arr_keys
for arr_key in $arr_keys
do
json_select "$arr_key"
json_get_vars dev UUID VERSION TYPE NAME LABEL total used ava use_age mount_point is_mounted enabled
cat <<EOF
			<tr>
				<td>
EOF
					echo "dev:<font color="#4335DA">"${dev}"</font>"
					[ $is_mounted -eq 1 ] && echo "<font color="#003300">已挂载</font>" || echo "未挂载"
					echo "<br/>label:"${LABEL}
					[ -n "${UUID}" ] && echo "<br/>uuid:"${UUID}
					[ -n "${TYPE}" ] && echo "<br/>type:"${TYPE}

cat <<EOF
				</td>
				<td>
    <div class="pure-u-1 progressdown">
EOF
if
[ -n "${use_age}" ]
then
use_age_num=`echo ${use_age} | grep -Eo '[0-9]*'`
cat <<EOF
		<div class="bar `[ $use_age_num -gt 80 ] && echo red || echo green`">
			<div style="width: ${use_age}">已用${use_age}</div>
		</div>
	</div>
	总量:`main.sbin storage_size_conver $(expr ${total} \* 1024)`
	(已用:`[ -n "${used}" ] && main.sbin storage_size_conver $(expr ${used} \* 1024)`空闲:`[ -n "${ava}" ] && main.sbin storage_size_conver $(expr ${ava} \* 1024)`)
EOF
else
cat <<EOF
总量:`main.sbin storage_size_conver $(expr ${total} \* 1024)`
EOF
fi
dev_set=`echo ${dev} | tr '/' '_'`
cat <<EOF
				</td>
				<td>
<script>
\$(function(){
  \$('#post_disk_set_${dev_set}').on('submit', function(e){
    e.preventDefault();
    var data = "app=disk&action=post_disk_set&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_disk_set_${dev_set}');
  });
});
</script>
<form class="pure-form" id="post_disk_set_${dev_set}">
EOF
if
echo "${TYPE}" | grep -qE "ext[2-4]|fat|ntfs"
then
cat <<EOF
				<input type="hidden" name="UUID" value="${UUID}"/>
				<input type="hidden" name="dev" value="${dev}"/>
				<input type="text" placeholder="/mnt/sdax" name="target" value="${mount_point}">
				<br/>
				<select name="enabled">
					<option value="0" `[ ${enabled} -ne 1 ] 2>/dev/null && echo selected`>卸载</option>
					<option value="1" `[ ${enabled} -eq 1 ] 2>/dev/null && echo selected`>挂载</option>
				</select>
				<br/>
				<button type="submit" class="pure-button">保存</button>
EOF
elif
[ "${TYPE}" = "swap" ]
then
cat <<EOF
				<input type="hidden" name="UUID" value="${UUID}"/>
				<input type="hidden" name="dev" value="${dev}"/>
				<select name="enabled">
					<option value="0" `[ ${enabled} -ne 1 ] && echo selected`>卸载</option>
					<option value="1" `[ ${enabled} -eq 1 ] && echo selected`>挂载</option>
				</select>
				<br/>
				<button type="submit" class="pure-button">保存</button>
EOF
fi
cat <<EOF
</form>
				</td>
			</tr>
EOF
json_close_object
done
cat <<EOF
		</tbody>

	</table>
EOF

cat <<EOF
			</div>
		</div>
EOF
footer_html
}
post_disk_basesetting()
{
uci set fstab.@global[0].anon_swap="$FORM_anon_swap" 2>&1 >/dev/null
uci set fstab.@global[0].anon_mount="$FORM_anon_mount" 2>&1 >/dev/null
uci set fstab.@global[0].check_fs="$FORM_check_fs" 2>&1 >/dev/null
uci commit fstab
echo "基础设置变动成功!!需要重启系统!!" | main.sbin output_json 0
}
post_disk_set()
{
# FORM_UUID=79f6f735-cb41-d001-00d6-f635cb41d001
# FORM_dev=/dev/sda1
# FORM_app=disk
# FORM_enabled=1
# FORM_action=post_disk_set
# FORM_target=/mnt/sda1


# FORM_UUID=
# FORM_dev=/dev/sda2
# FORM_app=disk
# FORM_enabled=1
# FORM_action=post_disk_set
uci_str=`uci -X show fstab`
[ -n "$FORM_UUID" ] || [ -n "$FORM_dev" ] || echo "没有选定设备!!" | main.sbin output_json 1

uci_config=`echo "$uci_str" | grep -E "\.uuid=${FORM_UUID}$" | cut -d '.' -f1,2`
[ -z "${uci_config}" ] && uci_config=`echo "$uci_str" | grep -E "\.device=${FORM_dev}$" | cut -d '.' -f1,2`
uci delete ${uci_config}
if
[ -n "$FORM_target" ]
then
cfg=$(uci add fstab mount)
uci set fstab.${cfg}=mount
uci set fstab.${cfg}.target=$FORM_target
[ -n "$FORM_UUID" ] && uci set fstab.${cfg}.uuid=$FORM_UUID
[ -n "$FORM_dev" ] && uci set fstab.${cfg}.device=$FORM_dev
uci set fstab.${cfg}.enabled=$FORM_enabled
else
cfg=$(uci add fstab swap)
uci set fstab.${cfg}=swap
[ -n "$FORM_UUID" ] && uci set fstab.${cfg}.uuid=$FORM_UUID
[ -n "$FORM_dev" ] && uci set fstab.${cfg}.device=$FORM_dev
uci set fstab.${cfg}.enabled=$FORM_enabled
fi
uci commit fstab
/etc/init.d/fstab stop 2>&1 >/dev/null
/etc/init.d/fstab start 2>&1 >/dev/null
echo "磁盘 ${FORM_dev} 设置成功!!" | main.sbin output_json 0
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi

?>